package marmalius.item;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import marmalius.PlayerHandler;
import marmalius.map.MapService;
import marmalius.tools.Config;
import marmalius.tools.DBService;
import marmalius.tools.LogService;

public class ItemGroundService {
	private static final ItemGroundService instance = new ItemGroundService();
	private List<ItemGround> itemGrounds;
	private List<ItemDrop> itemDrops;
	
	public ItemGroundService() {
		itemGrounds = new ArrayList<ItemGround>();
		itemDrops = new ArrayList<ItemDrop>();
	}
	
	public boolean loadItemSpawn() {
		int nb = 0;
		try {
			ResultSet rs = DBService.getInstance().doQuery("SELECT * FROM item_spawn");
			while (rs.next()) {
				itemGrounds.add(new ItemGround(rs.getInt("itemid"), rs.getInt("coordx"), rs.getInt("coordy"), rs.getByte("heightlevel"), rs.getByte("itemcount")));
				nb++;
			}
		}
		catch (SQLException ex) {
			LogService.getInstance().logError("Can't load item_spawn");
			LogService.getInstance().logError(ex.getMessage());
			return false;
		}
		
		for (ItemGround item : itemGrounds)
			MapService.getInstance().addObject(item);
		
		LogService.getInstance().logInfo("loaded "+nb+" item_spawn");
		return true;
	}
	
	public void process(int time) {
		for (ItemGround item : itemGrounds) {
			if (item.getRespawnTime() > 0) {
				if (item.getRespawnTime() - time > 0)
					item.setRespawnTime(item.getRespawnTime() - time);
				else {
					item.setRespawnTime(0);
					MapService.getInstance().addObject(item);
					PlayerHandler.getInstance().createGroundItemAll(item.getItemID(), item.getCoordX(), item.getCoordY(), item.getHeightLevel(), item.getItemCount());
				}
			}
		}
		List<ItemDrop> needToDelete = new ArrayList<>();
		for (ItemDrop item : itemDrops) {
			if (item.getRemoveTime() - time > 0)
				item.setRemoveTime(item.getRemoveTime() - time);
			else {
				if (!item.needRemove()) {
					item.setRemoveTime(Config.ITEMREMOVETIME);
					item.setNeedRemove();
				}
				else {
					MapService.getInstance().removeObject(item);
					needToDelete.add(item);
					PlayerHandler.getInstance().removeGroundItemAll(item.getItemID(), item.getCoordX(), item.getCoordY(), item.getHeightLevel());
				}
			}
		}
		for (ItemDrop item : needToDelete)
			itemDrops.remove(item);
	}
	
	public void pickUpItem(ItemGround item) {
		PlayerHandler.getInstance().removeGroundItemAll(item.getItemID(), item.getCoordX(), item.getCoordY(), item.getHeightLevel());
		item.setRespawnTime(Config.ITEMRESPAWNTIME);
		MapService.getInstance().removeObject(item);
	}
	
	public void pickUpItem(ItemDrop item) {
		PlayerHandler.getInstance().removeGroundItemAll(item.getItemID(), item.getCoordX(), item.getCoordY(), item.getHeightLevel());
		MapService.getInstance().removeObject(item);
		itemDrops.remove(item);
	}
	
	public ItemGround getItemGround(int itemID, int itemX, int itemY) {
		for (ItemGround item : itemGrounds) {
			if (item.getItemID() == itemID && item.getCoordX() == itemX && item.getCoordY() == itemY && item.getRespawnTime() == 0)
				return item;
		}
		return null;
	}
	
	public ItemDrop getItemDrop(int itemID, int itemX, int itemY) {
		for (ItemDrop item : itemDrops) {
			if (item.getItemID() == itemID && item.getCoordX() == itemX && item.getCoordY() == itemY)
				return item;
		}
		return null;
	}
	
	public void addDropItem(ItemDrop item, boolean showToAll) {
		itemDrops.add(item);
		item.setRemoveTime(Config.ITEMREMOVETIME);
		item.setNeedRemove();
		PlayerHandler.getInstance().createGroundItemAll(item.getItemID(), item.getCoordX(), item.getCoordY(), item.getHeightLevel(), item.getItemCount());
		MapService.getInstance().addObject(item);
	}
	
	public static ItemGroundService getInstance() { return instance; }
}
